package com.yunwushanhe.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.yunwushanhe.annotation.SystemLog;
import com.yunwushanhe.bean.req.CategoryAddReq;
import com.yunwushanhe.bean.req.CategoryPageListReq;
import com.yunwushanhe.bean.req.CategoryUpdateReq;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.*;
import com.yunwushanhe.entity.Category;
import com.yunwushanhe.enums.AppHttpCodeEnum;
import com.yunwushanhe.service.CategoryService;
import com.yunwushanhe.util.BeanCopyUtil;
import com.yunwushanhe.util.WebUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类表(Category)表控制层
 *
 * @author 抬头看看天
 * @since 2023-08-22 22:09:23
 */
@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult<List<CategoryAdminVO>> listAllCategory(){
        return categoryService.listAllCategory();
    }

    @PreAuthorize("@aps.hasPermission('content:category:export')")
    @GetMapping("/export") // 此接口用于导出excel文件
    public void export(HttpServletResponse response){
        try {
            // 设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            // 获取需要导出的数据
            List<Category> categoryList = categoryService.list();
            List<CategoryExcelVO> categoryExcelVOList = BeanCopyUtil.copyBeanList(categoryList, CategoryExcelVO.class);
            // 把数据写入到excel中
            EasyExcel.write(response.getOutputStream(), CategoryExcelVO.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(categoryExcelVOList);
        } catch (Exception e) {
            // 重置 response ，相当于是在清空 response 里面残留的数据
            response.reset();
            // 如果出现异常则响应json数据给前端
            ResponseResult<Object> responseResult = ResponseResult.error(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(responseResult));
        }
    }

    @GetMapping("/list")
    @SystemLog(businessName = "分页查询分类列表的接口能根据\n" +
                                "分类名称进行模糊查询；\n" +
                                "能根据状态进行查询。")
    public ResponseResult<PageVO<List<CategoryPageListVO>>> getCategoryPageList(CategoryPageListReq categoryPageListReq){
        return categoryService.getCategoryPageList(categoryPageListReq);
    }

    @PostMapping
    @SystemLog(businessName = "新增分类类别的接口")
    public ResponseResult<Object> addCategory(@RequestBody CategoryAddReq categoryAddReq){
        return categoryService.addCategory(categoryAddReq);
    }

    @GetMapping("/{id}")
    @SystemLog(businessName = "根据id查询所指定类别信息的接口，用于类别信息修改回显")
    public ResponseResult<CategoryUpdateVO> getCategoryUpdateById(@PathVariable("id") Long id){
        return categoryService.getCategoryUpdateById(id);
    }

    @PutMapping
    @SystemLog(businessName = "更新类别的接口")
    public ResponseResult<Object> updateCategoryById(@RequestBody CategoryUpdateReq categoryUpdateReq){
        return categoryService.updateCategoryById(categoryUpdateReq);
    }

    @DeleteMapping("/{id}")
    @SystemLog(businessName = "删除类别的接口")
    public ResponseResult<Object> deleteCategoryById(@PathVariable("id") String id){
        return categoryService.deleteCategoryById(id);
    }

}

